package com.chucang.shucang.log.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.chucang.shucang.common.base.page.PageData;
import com.chucang.shucang.common.base.page.PageQuery;
import com.chucang.shucang.common.base.service.impl.CrudServiceImpl;
import com.chucang.shucang.log.dao.CollectionConsignHistoryDao;
import com.chucang.shucang.log.dto.CollectionConsignHistoryDTO;
import com.chucang.shucang.log.entity.CollectionConsignHistoryEntity;
import com.chucang.shucang.log.service.CollectionConsignHistoryService;
import com.chucang.shucang.log.vo.CollectionConsignPageResVO;
import com.chucang.shucang.log.vo.query.CollectionConsignQuery;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;


/**
 * 藏品寄售记录表
 *
 * @author flitsneak flitsneak@gmail.com
 * @since 1.0.0 2022-09-12
 */
@Service
public class CollectionConsignHistoryServiceImpl extends CrudServiceImpl<CollectionConsignHistoryDao, CollectionConsignHistoryEntity, CollectionConsignHistoryDTO> implements CollectionConsignHistoryService {

    @Override
    public PageData<CollectionConsignPageResVO> consignPage(CollectionConsignQuery collectionConsignQuery) {
        LambdaQueryWrapper<CollectionConsignHistoryEntity> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(CollectionConsignHistoryEntity::getCollectionId, collectionConsignQuery.getId())
                .eq(CollectionConsignHistoryEntity::getSerialNum, collectionConsignQuery.getSerialNum())
                .select();
        IPage<CollectionConsignHistoryEntity> page = this.page(
                new PageQuery<CollectionConsignHistoryEntity>().getPage(collectionConsignQuery),
                lambdaQueryWrapper
        );
        if (CollUtil.isEmpty(page.getRecords())) {
            return new PageData<>(Collections.emptyList(), 0);
        }
        List<CollectionConsignPageResVO> consignPageResVOS = BeanUtil.copyToList(page.getRecords(), CollectionConsignPageResVO.class);
        return new PageData<>(consignPageResVOS, page.getTotal());
    }
}